package org.pp.core;


import lombok.Data;

import java.io.Serializable;

/**
 * @author rob
 * @date 2022-05-17
 */

@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -8270869343400458373L;

    private static int failCode = -1;
    private static String okMsg = "操作成功";
    private static String failMsg = "操作失败";

    private int code = 200;
    private String message = okMsg;
    private T data = null;

    private Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    private Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    private Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public static <T> Result<T> ok() {
        return new Result<T>();
    }

    public static <T> Result<T> ok(T t) {
        return new Result<T>().setData(t);
    }

    public static <T> Result<T> fail() {
        return fail(failCode);
    }

    public static <T> Result<T> fail(int code) {
        return fail(code, failMsg);
    }

    public static <T> Result<T> fail(String msg) {
        return fail(failCode, msg);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return new Result<T>().setMessage(msg).setCode(code);
    }

    public static <T> Result<T> fail(BizErro erro) {
        return new Result<T>().setMessage(erro.getMsg()).setCode(erro.getCode());
    }

    public static <T> Result<T> fail(T t) {
        return fail(failCode,t);
    }

    public static <T> Result<T> fail(int code, T t) {
        return fail(code, t, failMsg);
    }

    public static <T> Result<T> fail(int code, T t, String msg) {
        return new Result<T>().setCode(code).setData(t).setMessage(msg);
    }
}
